package com.kmproject.haifu.wxapi;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.jess.arms.utils.ArmsUtils;

import com.kmproject.haifu.mvp.model.api.Api;
import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

import org.simple.eventbus.EventBus;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";

    private IWXAPI api;

    //TODO　这里需要替换你的APP_ID
//    private String APP_ID = "wx0xxxxxxxxx"; //这里需要替换你的APP_ID

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.pay_result);

        api = WXAPIFactory.createWXAPI(this, Api.WXAPPID);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
    }

    @Override
    public void onResp(BaseResp baseResp) {
        Log.d("WX", "onPayFinish, errCode = " + baseResp.errCode);
        if (baseResp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            EventBus.getDefault().post(baseResp);
            switch (baseResp.errCode) {
                case BaseResp.ErrCode.ERR_OK:
                    ArmsUtils.makeText(this, "支付成功");
                    break;
                case BaseResp.ErrCode.ERR_COMM:
                    ArmsUtils.makeText(this, "支付发生错误");
                    break;
                case BaseResp.ErrCode.ERR_USER_CANCEL:
                    ArmsUtils.makeText(this, "取消支付");
                    break;
                default:
                    ArmsUtils.makeText(this, "支付返回");
                    break;
            }
            finish();
        }
    }
}